package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.JsonResult.JsonResult;
import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.cache.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.cache.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.cache.service.impl.IUserInfoRedisServiceImpl;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("users")
public class UserInfoController {
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @Value("${sms.url}")
    private String smsUrl;
    @Value("${sms.appKey}")
    private String appKey;

    @RequestMapping
    public Object get(String id) {
        UserInfo userInfo = userInfoService.get(id);
        return userInfo;
    }

    @GetMapping("/checkPhone")
    public Object checkPhone(String phone) {
        boolean ret = userInfoService.checkPhone(phone);
        return !ret;
    }

    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone) {
        userInfoService.sendVerifyCode(phone,smsUrl,appKey);
        return null;

    }

    @PostMapping("/regist")
    public Object regist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        userInfoService.regist(phone, nickname, password, rpassword, verifyCode);
        return JsonResult.success();
    }

    @PostMapping("/login")
    public Object login(String username, String password) {

        //登录成功之后返回用户对象
        UserInfo user=userInfoService.login(username, password);
        //创建token，将token添加到redis上2
        String token=userInfoRedisService.setToken(user);
        //页面需要：token   user
        Map<String,Object> map=new HashMap<>();
        map.put("token",token);
        map.put("user",user);
        return JsonResult.success(map);
    }

    //假设 页面发起请求带有token这个请求
    @GetMapping("/currentUser")
    public Object currentUser(HttpServletRequest request) {
            String token=request.getHeader("token");
            //通过token从redis中获取用户信息
            UserInfo user=userInfoRedisService.getUserByToken(token);
            System.out.println(user);
            return JsonResult.success(user);
    }
    @RequireLogin
    @GetMapping("/strategies/favor")
    public Object strategyFavor(String userId){
        return JsonResult.success(strategyStatisVORedisService.getUserStrategyFavor(userId));
    }
}
